package com.study.sfd.medicine.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.sfd.medicine.dao.SpecificationImgDao;
import com.study.sfd.medicine.entity.SpecificationImgEntity;
import com.study.sfd.medicine.service.SpecificationImgService;
import com.study.common.vo.MedicineDetail;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service("specificationImgService")
public class SpecificationImgServiceImpl extends ServiceImpl<SpecificationImgDao, SpecificationImgEntity> implements SpecificationImgService
{
    @Transactional
    public void saveBatch(MedicineDetail medicineWithImgs)
    {
        List<SpecificationImgEntity> specificationImgs = medicineWithImgs.getSpecificationImgs()
                .stream()
                .map(url -> SpecificationImgEntity
                        .builder()
                        .medicineId(medicineWithImgs.getId())
                        .url(url)
                        .build()
                ).collect(Collectors.toList());
        this.saveBatch(specificationImgs);
    }

    @Override
    public List<String> getImgUrlsByMdcId(Integer id)
    {
        return this.lambdaQuery()
                .eq(SpecificationImgEntity::getMedicineId, id)
                .list()
                .stream()
                .map(SpecificationImgEntity::getUrl)
                .collect(Collectors.toList());
    }
}